﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Threading;
using Business;

namespace TimeWork.webmaster.report
{
    public partial class TimeTotal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["mail"] == null)
            {
                Response.Redirect("http://" + Request.Url.Authority + "/webmaster/SignIn.aspx");
            }
            if (!IsPostBack)
            {
                rbMonth.Checked = true;
                showControl(true);
                loadContent();
            }
        }

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["companyid"] = ddlCompany.SelectedValue;
        }

        private void loadContent()
        {
            ddlCompany.DataSource = BuCompany.selAllCompany();
            ddlCompany.DataTextField = "CompanyName";
            ddlCompany.DataValueField = "CompanyID";
            ddlCompany.DataBind();
            if (Session["companyid"] == null)
            {
                Session["companyid"] = "1";
            }
            
            ddlCompany.SelectedValue = Session["companyid"].ToString(); 
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }
        protected void btnExportExcel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AppendHeader("Content-Disposition", "attachment;filename=ThoiGianSDTheoSP" + DateTime.
            Now.ToString("yyyyMMdd") + ".xls");
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;

            var sb = new StringBuilder();
            ltrData.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
            Response.Write(sb.ToString());
            Response.End();
        }

        protected void rbMonth_CheckedChanged(object sender, EventArgs e)
        {
            showControl(true);
        }

        protected void rbYear_CheckedChanged(object sender, EventArgs e)
        {
            showControl(false);
        }

        private void showControl(bool cond)
        {
            if (cond)
            {
                ddlMonth.Visible = true;
                ddlYear.Visible = true;
                ddlTimeYear.Visible = false;
            }
            else
            {
                ddlMonth.Visible = false;
                ddlYear.Visible = false;
                ddlTimeYear.Visible = true;
            }
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            ltrData.Text = String.Empty;
            Thread.Sleep(500);
            int level = Int32.Parse(ddlCode.SelectedValue);

            if (rbMonth.Checked)
            {
                int month = Int32.Parse(ddlMonth.SelectedValue);
                int year = Int32.Parse(ddlYear.SelectedValue);
                ltrData.Text = BuReport.repTimeTotalAdmin(month, year, true, level, cbIsChild.Checked, Session["companyid"].ToString());
            }
            else
            {
                int timeYear = Int32.Parse(ddlTimeYear.SelectedValue);
                ltrData.Text = BuReport.repTimeTotalAdmin(1, timeYear, false, level, cbIsChild.Checked, Session["companyid"].ToString());
            }
        }

        
    }
}